package com.wangshuqi.spring.service.impl;

import com.wangshuqi.spring.dao.AccountDAO;
import com.wangshuqi.spring.entity.Account;
import com.wangshuqi.spring.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDAO accountDAO;

    @Transactional(
            rollbackFor = Exception.class, // 事务的回滚条件
            propagation = Propagation.REQUIRED, // 事务的传播属性
            isolation = Isolation.READ_COMMITTED // 事务的隔离级别
    )
    @Override
    public int operateMoney(int number) {
        // 张三加100
        Account zhangsan = new Account();
        zhangsan.setName("张三");
        zhangsan.setMoney(100);
        accountDAO.addMoney(zhangsan);

        int i = 10 / number;

        // 李四扣减100
        Account lisi = new Account();
        lisi.setName("李四");
        lisi.setMoney(100);
        accountDAO.reduceMoney(lisi);

        return 1;
    }
}
